import { ref } from 'vue';
import api from 'axios';
import { sysCatalogType, sysCatalogUrlAPI } from 'src/util/SysBaseConst';
import { cataItem } from 'src/hook/common/useCatalogType';

export function useSysCatalogTbl () {
  const companyTypeList = ref<cataItem[]>([]);//公司类型列表
  const statusTypeList = ref<cataItem[]>([]); //状态列表
  const sexTypeList = ref<cataItem[]>([]);   //性别列表
  const positionTypeList = ref<cataItem[]>([]);//职位列表
  const departmentTypeList = ref<cataItem[]>([]); //部门列表
  const graphTypeList = ref<cataItem[]>([]);//图形列表(icon,avatar, graph)
  const menuTypeList = ref<cataItem[]>([]); //菜单类型列表（目录和菜单）
  const iconTypeList = ref<cataItem[]>([]); //icon分类
  const tupleTypeList = ref<cataItem[]>([]); //tuple分类
  const fieldTypeList = ref<cataItem[]>([]); //行业分类
  const permissionComponentTypeList = ref<cataItem[]>([]); //权限组件列表
  const permissionOperateTypeList = ref<cataItem[]>([]); //权限操作列表

  const departmentList = ref<cataItem[]>([]); //部门列表
  const companyList = ref<cataItem[]>([]); //公司列表
  const roleList = ref<cataItem[]>([]); //角色列表
  const menuList = ref<cataItem[]>([]);//菜单列表
  const iconList = ref<cataItem[]>([]); //公司列表
  const tupleList = ref<cataItem[]>([]); //图元列表
  const dictTypeList = ref<cataItem[]>([]); //字典分类列表

  //路径
  let url = ref('');

  //从后台读取目录
  const getCatalog = (catalog: sysCatalogType) => {
    switch (catalog) {
      case sysCatalogType.SEX_TYPE_CODE:
        url.value = sysCatalogUrlAPI.SEX_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            sexTypeList.value = res.data.data;
          }
        })
        break;
      case sysCatalogType.MENU_TYPE_CODE:
        url.value = sysCatalogUrlAPI.MENU_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            menuTypeList.value = res.data.data;
          }
        })
        break;
      case sysCatalogType.STATUS_TYPE_CODE:
        url.value = sysCatalogUrlAPI.STATUS_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            statusTypeList.value = res.data.data;
          }
        })
        break;
      case sysCatalogType.POSITION_TYPE_CODE:
        url.value = sysCatalogUrlAPI.POSITION_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            positionTypeList.value = res.data.data;
          }
        })
        break;
      case sysCatalogType.COMPANY_TYPE_CODE:
        url.value = sysCatalogUrlAPI.COMPANY_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            companyTypeList.value = res.data.data;
          }
        })
        break;
      case sysCatalogType.GRAPH_TYPE_CODE:
        url.value = sysCatalogUrlAPI.GRAPH_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            graphTypeList.value = res.data.data;
          }
        })
        break;
      case sysCatalogType.ICON_TYPE_CODE:
        url.value = sysCatalogUrlAPI.ICON_TYPE_CODE;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            iconTypeList.value = res.data.data;
          }
        })
        break;
      case sysCatalogType.TUPLE_TYPE_CODE:
        url.value = sysCatalogUrlAPI.TUPLE_TYPE_CODE;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            tupleTypeList.value = res.data.data;
          }
        })
        break;
      case sysCatalogType.FIELD_TYPE_CODE:
        url.value = sysCatalogUrlAPI.FIELD_TYPE_CODE;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            fieldTypeList.value = res.data.data;
          }
        })
        break;

      case sysCatalogType.PERMISSION_COMPONENT_TYPE_CODE:
        url.value = sysCatalogUrlAPI.PERMISSION_COMPONENT_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            permissionComponentTypeList.value = res.data.data;
          }
        })
        break;

      case sysCatalogType.PERMISSION_OPERATE_TYPE_CODE:
        url.value = sysCatalogUrlAPI.PERMISSION_OPERATE_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            permissionOperateTypeList.value = res.data.data;
          }
        })
        break;

      case sysCatalogType.ROLE_CODE:
        url.value = sysCatalogUrlAPI.ROLE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            roleList.value = res.data.data;
          }
        })
        break;
      case sysCatalogType.DICT_TYPE_CODE:
        url.value = sysCatalogUrlAPI.DICT_TYPE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            dictTypeList.value = res.data.data;
          }
        })
        break;


      case sysCatalogType.DEPARTMENT_CODE:
        url.value = sysCatalogUrlAPI.DEPARTMENT_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            departmentTypeList.value = res.data.data;
          }
        })
        break;
      case sysCatalogType.COMPANY_CODE:
        url.value = sysCatalogUrlAPI.COMPANY_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            companyList.value = res.data.data;
          }
        })
        break;
      case sysCatalogType.MENU_CODE:
        url.value = sysCatalogUrlAPI.MENU_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            menuList.value = res.data.data;
          }
        })
        break;
      case sysCatalogType.ICON_CODE:
        url.value = sysCatalogUrlAPI.ICON_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            iconList.value = res.data.data;
          }
        })
        break;
      case sysCatalogType.TUPLE_CODE:
        url.value = sysCatalogUrlAPI.TUPLE_API;
        api.post(url.value).then((res) => {
          if ((res.data) && (res.data.message === 'Success') && (res.data.data)) {
            tupleList.value = res.data.data;
          }
        })
        break;
      default:
        break;
    }
  };


  //释放目录
  const removeCatalog = (catalog: sysCatalogType) => {
    switch (catalog) {
      case sysCatalogType.SEX_TYPE_CODE:
        sexTypeList.value.length = 0;
        break;
      case sysCatalogType.MENU_TYPE_CODE:
        menuTypeList.value.length = 0;
        break;
      case sysCatalogType.STATUS_TYPE_CODE:
        statusTypeList.value.length = 0;
        break;
      case sysCatalogType.POSITION_TYPE_CODE:
        positionTypeList.value.length = 0;
        break;
      case sysCatalogType.COMPANY_TYPE_CODE:
        companyTypeList.value.length = 0;
        break;
      case sysCatalogType.GRAPH_TYPE_CODE:
        graphTypeList.value.length = 0;
        break;
      case sysCatalogType.ICON_TYPE_CODE:
        iconTypeList.value.length = 0;
        break;
      case sysCatalogType.TUPLE_TYPE_CODE:
        tupleTypeList.value.length = 0;
        break;
      case sysCatalogType.FIELD_TYPE_CODE:
        fieldTypeList.value.length = 0;
        break;
      case sysCatalogType.PERMISSION_COMPONENT_TYPE_CODE:
        permissionComponentTypeList.value.length = 0;
        break;
      case sysCatalogType.PERMISSION_OPERATE_TYPE_CODE:
        permissionOperateTypeList.value.length = 0;
        break;

      case sysCatalogType.ROLE_CODE:
        roleList.value.length = 0;
        break;
      case sysCatalogType.DICT_TYPE_CODE:
        dictTypeList.value.length = 0;
        break;
      case sysCatalogType.DEPARTMENT_CODE:
        departmentTypeList.value.length = 0;
        break;
      case sysCatalogType.COMPANY_CODE:
        companyList.value.length = 0;
        break;
      case sysCatalogType.MENU_CODE:
        menuList.value.length = 0;
        break;
      case sysCatalogType.ICON_CODE:
        iconList.value.length = 0;
        break;

      default:
        break;
    }
  }
  return {
    sexTypeList,
    menuTypeList,
    statusTypeList,
    departmentTypeList,
    positionTypeList,
    companyTypeList,
    graphTypeList,
    iconTypeList,
    tupleTypeList,
    fieldTypeList,
    permissionComponentTypeList,
    permissionOperateTypeList,

    departmentList,
    roleList,
    dictTypeList,
    companyList,
    menuList,
    iconList,
    tupleList,

    getCatalog,
    removeCatalog
  }
}
